
import { ControlHelper } from '../controls';
import * as typings from '../typings';
import { FormSheet } from 'h3-forms';

import * as SystemCodes from '../../schema/system-data-item-codes';

import { DataItemType, RelevanceFormSelectMode } from '../typings';

import { FormStatusZh, FormStatusEn } from '../../schema';

import * as common  from '@cloudpivot/common';
import * as moment from "moment";

import {
    RelevanceFormControl,
    ReverseRelevanceControl
  } from "../controls";

export abstract class ReverseQueryService {

  static async getListQueryConditions(schemaCode: string,queryCode: string, params:any) {
    
    if (!RelevanceFormControl.service.getListQueryConditions) {
        return Promise.reject(null);
    }

    return await RelevanceFormControl.service.getListQueryConditions(schemaCode, queryCode, params);
    // return await getList(schemaCode, queryCode, params);
  }

    static getParams(conditions:any,formControls:any, c:any,findController:any) {
    if (!conditions) {
        return [];
    }
  
    const params = conditions.split(';').map(arr => {
        const _index = arr.indexOf(":");
        let f1 =  arr.slice(0, _index);
        let f2 = arr.substring(_index + 1);
        if (!f2) {
            return null;
        }

        if (f2[0] === '{') {
            f2 = f2.substr(1, f2.length - 2);

            let ctrl: any;
            let control: typings.RendererFormControl | undefined;

            const idx = f2.indexOf('.');
            if (idx > -1) {
                const sheetKey = f2.substring(0, idx);
                const controlKey = f2.substr(idx + 1);

                const sheet = formControls.find((child: any) => child.key === sheetKey) as any as typings.FormSheet;
                if (sheet) {
                    control = sheet.columns.find(child => child.key === controlKey) as any;
                }

                const sheetCtrl = findController(f2.substring(0, idx)) as FormSheet;
                if (sheetCtrl && c.options && typeof c.options.rowIndex === 'number') {
                    const row = sheetCtrl.rows[c.options.rowIndex];
                    ctrl = row.findChild(f2.substr(idx + 1));
                }
            } else {
                ctrl = findController(f2);
                control = formControls.find((child: any) => child.key === f2);
            }

            if (!ctrl || !control) {
                return null;
            }

            return ({
                code: f1,
                value: ctrl.value,
                type: ControlHelper.mapToDataItemType(control.type)
            });
        } else {
            return {
                code: f1,
                value: f2
            };
        }

    }).filter(x => x !== null);

        return params as any[];
    }

    static formatQueryData(queryCondition:any[]) {
        return queryCondition.map(res => {
            const type = res.propertyType;
            const code = res.propertyCode;
            let val:any = '';
            switch(res.propertyType){
                case DataItemType.Logic:
                    if(res.defaultState === 1) {
                        val = true;
                    } else {
                        val = false;
                    }
                    break;
                case DataItemType.Date:
                   if (res.defaultValue) {
                    val = res.defaultValue.map((x:any) => {
                      if(typeof x === 'string'){
                        return x;
                      }else if(x instanceof Date){
                        try{
                          return (moment as any).default(x).format('YYYY-MM-DD');
                        }catch {
                          return '';
                        }
                      }
                      return '';
                    }).join(';');
                   }

                    break
                default:
                    val = res.defaultValue;
                    break;
            }

            if (res.propertyCode === SystemCodes.sequence_status && res.defaultValue) {
                const status_en:string[] = [];

                const statusArr = res.defaultValue.split(';');
                  statusArr.forEach(s => {
                    switch (s) {
                      case FormStatusZh.DRAFT:
                        status_en.push(FormStatusEn.DRAFT)
                        break;
                      case FormStatusZh.PROCESSING:
                         status_en.push(FormStatusEn.PROCESSING)
                        break;
                      case FormStatusZh.CANCELLED:
                        status_en.push(FormStatusEn.CANCELLED)
                        break;
                      case FormStatusZh.COMPLETED:
                        status_en.push(FormStatusEn.COMPLETED)
                        break;
                    
                      default:
                        break;
                    }
                  });
                  val = status_en.join(';');
            }
            return {
                code,
                type,
                value: val
            }

        });
    }

    // static fetch(schemaCode: string,queryCode:string,page: number, pageSize: number, value: string, params: any[]) {
    //     // if (!RelevanceFormControl.service) {
    //     //     return;
    //     // }

    //     // const schemaCode = this.controlOpts.schemaCode || '';
    //     // const queryCode = this.controlOpts.queryCode || '';

    //     // if (!schemaCode || !queryCode) {
    //     //     return;
    //     // }
    //     // const params = queryData ? [] : this.getParams();
    //     params.push({
    //         code: 'name',
    //         type: DataItemType.ShortText,
    //         value
    //     });

    //     // const _ctrl = this.ctrl as any;
    //     // const cols = _ctrl.options && _ctrl.options.mappings ? Object.keys(_ctrl.options.mappings) : [];
    //     const cols: any[] = [];
    //     return ReverseRelevanceControl.service.search(schemaCode, queryCode, params, cols, page, pageSize);
    // }
}

// export interface RelevanceFormSearchParams {
//   code: string
//   type: DataItemType
//   value: any
// }
